package cn.tiancaifan.web;

import cn.hutool.core.util.StrUtil;
import cn.tiancaifan.entity.SystemResult;
import cn.tiancaifan.entity.User;
import cn.tiancaifan.service.UserServiceI;
import cn.tiancaifan.service.impl.UserServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user.do")
public class UserServlet  extends HttpServlet {

    private UserServiceI userService = new UserServiceImpl();

    private Gson gson = new Gson();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/json;charset=utf-8");
        User parameters = getParameters(req);
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        if (user== null){
            String s = gson.toJson(SystemResult.error("请登录！"));
            PrintWriter out = resp.getWriter();
            out.println(s);
            return;
        }
        if (parameters == null){
            SystemResult allUser = userService.findAllUser(user.getId());
            String s = gson.toJson(allUser);
            PrintWriter out = resp.getWriter();
            out.println(s);
            return;
        }

        SystemResult userBycondition = userService.findUserBycondition(parameters, user.getId());
        String s = gson.toJson(userBycondition);
        PrintWriter out = resp.getWriter();
        out.println(s);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/json;charset=utf-8");

        HttpSession session = req.getSession();
        User user = (User)session.getAttribute("user");
        if (user== null){
            String s = gson.toJson(SystemResult.error("请登录！"));
            PrintWriter out = resp.getWriter();
            out.println(s);
            return;
        }
        long id = Long.parseLong(req.getParameter("id"));
        String nikename = req.getParameter("nikename");
        String strDistrictId = req.getParameter("districtId");
        long districtId = Long.parseLong(strDistrictId == null ? "0" : strDistrictId);
            String address = req.getParameter("address");
        String strRole = req.getParameter("role");
        long role = Long.parseLong(strRole == null ? "0" : strRole);
        String avatar = req.getParameter("avatar");
        String tel = req.getParameter("tel");
        User u = new User();
        u.setId(id);
        u.setNikename(nikename);
        u.setDistrictId(districtId);
        u.setAddress(address);
        u.setRole(role);
        u.setAvatar(avatar);
        u.setTel(tel);
        u.setUpdateId(user.getId());
        SystemResult systemResult = userService.updateUserById(u);
        String s = gson.toJson(systemResult);
        PrintWriter out = resp.getWriter();
        out.println(s);
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("我是put 请求");
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/json;charset=utf-8");

        String[] ids = req.getParameterValues("id");
        SystemResult systemResult = userService.deleteUserByid(ids);

        String s = gson.toJson(systemResult);
        PrintWriter out = resp.getWriter();
        out.println(s);
    }

    private User getParameters(HttpServletRequest req){
        User user = null;
        String account = req.getParameter("account");
        if (!StrUtil.hasEmpty(account)){
            if (user == null){
                user = new User();
            }
            user.setAccount(account);
        }
        String nikename = req.getParameter("nikename");

        if (!StrUtil.hasEmpty(nikename)){
            if (user == null){
                user = new User();
            }
            user.setNikename(nikename);
        }
        String role = req.getParameter("role");
        if (!StrUtil.hasEmpty(role)){
            if (user == null){
                user = new User();
            }
            user.setRole(Long.parseLong(role));
        }
        String districtId = req.getParameter("districtId");
        if (!StrUtil.hasEmpty(districtId)){
            if (user == null){
                user = new User();
            }
            user.setRole(Long.parseLong(districtId));
        }
        return user;
    }
}
